<?php
namespace app\admin\controller;

use app\admin\model\AdvisoryPerson;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\ProductInfo;
use app\admin\model\AdvisoryInfo;
class Teachs extends Rbac
{
//    教务部设计开发
    public function teachs()
    {
        $res = new ProductInfo();
        $result = $res->where('pro_sn',1)->paginate(10);
        $this->assign('result',$result);
       return $this->fetch('teachs/wish');
    }

    //    教务部设计开发详情
    public function sjdetaile()
    {
        $job_group_id = request()->param('pro_id');
//            $res = new AdvisoryInfo();
        $result = Db::table('mdm_job')->alias('j')
            ->join('mdm_product p','j.job_group_id = p.pro_id')
            ->where(['job_group_id'=>$job_group_id,'job_getout_sn'=>2])->select();
        for ($i=0;$i<count($result);$i++){
            if ($result[$i]['job_getout_sn']==0){
                $result[$i]['job_getout_sn']='离开';
            }else{
                $result[$i]['job_getout_sn']='在';
            }
        }
        $this->assign('result',$result);
        return $this->fetch('teachs/sjdetaile');
    }

    //    教务部设计修改页面
//->where('pro_date','2')
    public function sjupdate()
    {
        $job_id = request()->param('job_id');
        $res = new AdvisoryInfo();
        $result = $res->where('job_id',$job_id)->find()->toArray();
//        $time = time();
//        $timenow = date("Y-m-d",$time);
        $res = Db::table('mdm_product')->where('pro_date','2')->select();
        $this->assign('result',$result);
        $this->assign('res',$res);
        return $this->fetch('teachs/sjupdate');
    }
    //    教务部设计修改
    public function update()
    {
        $job_id = request()->param('job_id');
        $res = new AdvisoryInfo();
        $row = request()->param();
        $data = [
            'job_group_id'=>$row['pro_id']
        ];
        $results= $res->allowField(true)->save($data,['job_id'=>$job_id]);
        $result = $res->allowField(true)->save($row,['job_id'=>$job_id]);
        if ($result||$results){
            $this->success('修改成功','admin/teachs/teachs');
        }
    }

    //    弹出弹框查看详情页面
    public function test(){
        $job_id = request()->param('job_id');
        $result =  Db::table('mdm_job')->where('job_id',$job_id)->find();
        $this->assign('result',$result);
        return $this->fetch('teachp/test');
    }

    //    教务部设计组添加
    public function teachsadd()
    {
        $pro_id = request()->param('pro_id');
        if (empty($pro_id)){
            $row = request()->param();
            if ($row){
                $row['pro_createtime']=strtotime(request()->param('pro_createtime'));
            }
            $res = new ProductInfo();
            $result = $res->allowField(true)->save($row);
            if ($result){
                $this->success('添加成功','admin/teachs/teachs');
            }
        }
        $res = Db::table('mdm_user')->where('user_r_id',13)->select();
        $this->assign('res',$res);
        return $this->fetch('teachs/sjadd');
    }

//    教务部设计删除组
    public function del(){
         $pro_id = request()->param('id');
         $res = new ProductInfo();
         $row = $res->where('pro_id',$pro_id)->delete();
         if ($row){
             echo json_encode(['msg'=>200,'row'=>$row]);
         }else{
             echo json_encode(['msg'=>300]);
         }
    }

    //启用停用状态
    public function ban(){
        $request = Request::instance();
        $id = $request->param('id');
//        echo  $id;exit();
        $amininfo = new ProductInfo();
        $status = $amininfo->where('pro_id',$id)->value('pro_date');
        if ($status == 1 ){
            $status = 2;
        }else{
            $status = 1;
        }
        $res = $amininfo->save(['pro_date'=>$status],['pro_id'=>$id]);
//        var_dump($res);exit();
        if($res){
            echo json_encode(['msg'=>200]);
        }else{
            echo json_encode(['msg'=>300]);
        }
    }
}
